﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using GRWL.BZSC.UI.CustomCtrl;

namespace GRWL.BZSC.UI
{
    public partial class ProductionStockInFrm : BaseFrm
    {
        private bool bGraph = false;

        private void bindDepot()
        {
            //TB_Depot[] tbDepot = this.BaseDataProxy.FindDepots(null, null, null);
            //DataTable dtDepot = tbDepot.ToDataTable();

            //this.depot.Items.Add("[]");
            //for (int i = 0; i < dtDepot.Rows.Count; i++)
            //{
            //    this.depot.Items.Add(dtDepot.Rows[i]["name"] + "[" + dtDepot.Rows[i]["id"] + "]");
            //}
        }

        private void initDgvColumns()
        {
            this.dtpData.AutoGenerateColumns = false; 

            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);//0
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "shipping", "所属单头id", false, true, null);//1
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "shippingCode", "所属单头单号", false, true, null);//2
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "material", "物资id", true, true, null);//3
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "materialCode", "物资代号", true, true, null);//4
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "materialName", "物资名称", true, true, null);//5
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "specs", "规格", true, true, null);//6
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "shippingNum", "入库数量", true, false, null);//7
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "batchNo", "批次", true, true, null);//8
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "depot", "仓库id", false, true, null);//9
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "depotName", "仓库名", true, true, null);//注意：当改变顺序时，dtpData_CellDoubleClick方法要更改
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "memo", "备注", true, false, null);//11
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "qcId", "上一级单id", false, true, null);//12
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "qcCode", "上一级单代号", true, true, null);//13
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "order", "订单id", false, true, null);//14
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "orderCode", "订单号", true, true, null);//15
            Common.AddDgvColumn(this.dtpData, new DataGridViewTextBoxColumn(), "orderDetailId", "订单明细id", false, true, null);//16
        }

        private void SetNull()//设置为空
        {
            this.txtDjh.Text = "";
            this.txtOperator.Text = "";
            this.txtAccessor.Text = "";
            this.txtStatus.Text = "";
            this.txtDepartment.SetSelected(null);
            this.txtMemo.Text = "";
            this.txtMender.Text = "";

            for (int i = dtpData.Rows.Count - 1; i >= 0; i--)
            {
                dtpData.Rows.RemoveAt(i);
            }
        }

        private void setReadOnly(bool b)//设置只读
        {
            this.btnDelItem.Enabled = b;
            this.btnDelete.Enabled = b;
            this.btnPrint.Enabled = b;
            this.btnSave.Enabled = b;
            this.btnSelect.Enabled = b;
            this.btnConfirm.Enabled = b;
            this.txtDjh.Enabled = b;
            this.txtMemo.Enabled = b;

            //for (int i = 0; i < this.dtpData.Columns.Count; i++)
            //{
            //    if (b && (this.dtpData.Columns[i].Name == "t_num" || this.dtpData.Columns[i].Name == "t_depot" || this.dtpData.Columns[i].Name == "t_memo"))
            //    {
            //        this.dtpData.Columns[i].ReadOnly = false;
            //    }
            //    else
            //    {
            //        this.dtpData.Columns[i].ReadOnly = true;
            //    }
            //}
        }

        private void SetControlsStatus(bool SetEnagledFlag)
        {
 
        }

        public ProductionStockInFrm()
        {
            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();

            InitializeComponent();

            initDgvColumns();

            EditStatus = Common.BillOperateType.None;
        }

        private void ProductionStockInFrm_Load(object sender, EventArgs e)
        {
            //this.btnDelItem.Enabled = false;
            //this.btnDelete.Enabled = false;
            //this.btnPrint.Enabled = false;
            //this.btnSave.Enabled = false;
            //this.btnSelect.Enabled = false;
            //this.btnConfirm.Enabled = false;
            setReadOnly(false);
        }

        private void btnAppend_Click(object sender, EventArgs e)
        {
            //放弃新单
            if (EditStatus == Common.BillOperateType.Add)
            {
                EditStatus = Common.BillOperateType.None;
                SetControlsStatus(false);

                dtpData.CancelEdit();
                SetNull();
                return;
            }

            //新单输入
            setReadOnly(true);
            SetNull();
            EditStatus = Common.BillOperateType.Add;
            this.btnConfirm.Enabled = false;
            this.txtOperator.Text = SysLoginUserInfo.UserName;
            this.txtStatus.Text = "未批";

            tMain.Clear();
            DataRow NewMainRow = tMain.NewRow();
            NewMainRow["shippingDate"] = this.dtpOutDate.Value;
            NewMainRow["status"] = this.txtStatus.Text;

            tMain.Rows.Add(NewMainRow);
        }

        private void txtDepartment_SelectorClick(object sender, EventArgs e)
        {
            if ((EditStatus != Common.BillOperateType.Add) & (EditStatus != Common.BillOperateType.Update))
            {
                return;
            }

            DeptFrm frm = new DeptFrm();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtDepartment.ValueType = Selector.NodeValueType.Name;
                txtDepartment.SetSelected(frm.SelectorItem);

                tMain.Rows[0]["department"] = Common.StringToInt(frm.SelectorItem.Key);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.toolStrip1.Focus();
            if ((EditStatus != Common.BillOperateType.Add) & (EditStatus != Common.BillOperateType.Update) & (EditStatus == Common.BillOperateType.Delete))
                return;
            if (tMain.Rows.Count <= 0 || this.dtpData.Rows.Count <= 0)
            {
                MessageBox.Show("无单据要保存。。。");
                return;
            }

            if (EditStatus == Common.BillOperateType.Add)
            {
                if (string.IsNullOrEmpty(tMain.Rows[0]["code"].ToString()))
                {
                    this.txtDjh.Text = BaseDataProxy.GetSerialNo(1, "SCSH", 4, true, true, true);
                    tMain.Rows[0]["code"] = this.txtDjh.Text;
                }
                tMain.Rows[0]["operator"] = SysLoginUserInfo.UserId;
                tMain.Rows[0]["createTime"] = DateTime.Now;
                tMain.Rows[0]["shippingDate"] = this.dtpOutDate.Value;
                btnConfirm.Enabled = true;
            }
            else
            {
                tMain.Rows[0]["mender"] = SysLoginUserInfo.UserId;
                tMain.Rows[0]["updateTime"] = DateTime.Now;

                this.txtMender.Text = SysLoginUserInfo.UserName;

            }

            for (int i = 0; i < tDetail.Rows.Count; i++)
            {
                tDetail.Rows[i]["shippingCode"] = this.txtDjh.Text;
            }

            tMain.Rows[0]["memo"] = this.txtMemo.Text;
            tMain.Rows[0]["type"] = "SCSH";

            tMain.AcceptChanges();
            tDetail.AcceptChanges();

            TB_Shipping tbShipping = OTConverter.ConvertToObject<TB_Shipping>(tMain.Rows[0]);

            //if (EditStatus == Common.BillOperateType.Add || EditStatus == Common.BillOperateType.Update)
            //{
            //    tDetail.Clear();
            //    for (int i = 0; i < this.dtpData.Rows.Count; i++)
            //    {
            //        if (dtpData.RCText(i, "shippingNum").ToString() == "")
            //        {
            //            MessageBox.Show("入库数量不能为空！(^_^)");
            //            return;
            //        }
            //        DataRow NewDetailRow = tDetail.NewRow();

            //        if (this.dtpData.RCText(i, "id") != "")
            //            NewDetailRow["id"] = Convert.ToInt32(this.dtpData.RCText(i, "id"));
            //        //NewDetailRow["qc"] = Convert.ToInt32(this.gvQCDetail.RCText(i, "qc"));
            //        //NewDetailRow["shipping"] = this.txtDjh.Text;
            //        NewDetailRow["shippingCode"] = this.txtDjh.Text;
            //        NewDetailRow["material"] = Convert.ToInt32(this.dtpData.RCText(i, "material"));
            //        NewDetailRow["materialCode"] = this.dtpData.RCText(i, "materialCode");
            //        if (this.dtpData.RCText(i, "order") != "")
            //            NewDetailRow["order"] = Convert.ToInt32(this.dtpData.RCText(i, "order"));
            //        NewDetailRow["orderCode"] = this.dtpData.RCText(i, "orderCode");
            //        NewDetailRow["shippingNum"] = this.dtpData.RCText(i, "shippingNum");
            //        NewDetailRow["batchNo"] = this.dtpData.RCText(i, "batchNo");
            //        if (this.dtpData.RCText(i, "qcDetail") != "")
            //            NewDetailRow["qcDetail"] = this.dtpData.RCText(i, "qcDetail");
            //        NewDetailRow["depot"] = this.dtpData.RCText(i, "depot");
            //        NewDetailRow["memo"] = this.dtpData.RCText(i, "memo");
            //        if (this.dtpData.RCText(i, "orderDetailId") != "")
            //            NewDetailRow["orderDetailId"] = Convert.ToInt32(this.dtpData.RCText(i, "orderDetailId"));
            //        if (this.dtpData.RCText(i, "qcId") != "")
            //            NewDetailRow["qcId"] = this.dtpData.RCText(i, "qcId");
            //        NewDetailRow["qcCode"] = this.dtpData.RCText(i, "qcCode");
            //        tDetail.Rows.Add(NewDetailRow);
            //    }
            //}

            List<TB_ShippingDetail> lsShippingDetails = OTConverter.ConvertTableToObject<TB_ShippingDetail>(tDetail);
            TB_ShippingDetail[] tbShippingDetails = lsShippingDetails.ToArray();
            tbShipping.TB_ShippingDetail = tbShippingDetails;

            bool bUpdate = false;
            int iAddID = -1;

            if (EditStatus == Common.BillOperateType.Add)
            {
                iAddID = BaseDataProxy.AddShipping(tbShipping);
                tMain.Rows[0]["id"] = iAddID;
            }
            else
            {
                bUpdate = BaseDataProxy.UpdateShipping(tbShipping, true);
            }

            if (((EditStatus == Common.BillOperateType.Add) && (iAddID > 0)) || ((EditStatus == Common.BillOperateType.Update) && (bUpdate)))
            {
                MessageBox.Show("已成功保存单据.");
                EditStatus = Common.BillOperateType.Update;
                //OpenBill(Common.StringToInt(tMain.Rows[0]["id"].ToString()));
                ////SetControlsStatus(false);
            }
            else
                MessageBox.Show("保存单据失败.");

        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            FindBillFrm frm = new FindBillFrm();
            frm.AllowMulitSelect = false;
            frm.SelectBillType = GRWL.BZSC.Common.EnumCommon.BillType.CGSH;
            frm.BillPrefix = "SCSH";
            frm.Text = "选单(来自生产单)";

            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK)
            {
                if (frm.SelectedItems != null)
                {
                    bGraph = true;
                    for (int i = dtpData.Rows.Count - 1; i >= 0; i--)
                    {
                        dtpData.Rows.RemoveAt(i);
                    }
                    QShipping qsh = OTConverter.ConvertToObject<QShipping>(frm.SelectedItems);
                    //QRecvMaterial qrecv = OTConverter.ConvertToObject<QRecvMaterial>(frm.SelectedItems);
                    OpenBill(qsh.Id);
                    EditStatus = Common.BillOperateType.Update;
                    bGraph = false;

                    if (this.txtStatus.Text == "已批")
                        setReadOnly(false);
                    else if (this.txtStatus.Text == "未批")
                        setReadOnly(true);

                    this.btnPrint.Enabled = true;
                    this.btnConfirm.Enabled = true;
                }
            }
        }

        private void OpenBill(int BillId)
        {
            //XRecvMaterial tM = BaseDataProxy.GetXRecvMaterial(BillId);
            //XRecvDetail[] tD = BaseDataProxy.GetRecvDetails(BillId);

            XShipping tM = BaseDataProxy.GetXShipping(BillId);
            XShippingDetial[] tD = BaseDataProxy.GetShippingDetails(BillId);

            #region
            if (tM != null)
            {

                tMain.Clear();
                DataRow NewMainRow = tMain.NewRow();
                NewMainRow["id"] = tM.Shipping.id;

                this.txtDjh.Text = tM.Shipping.code;
                NewMainRow["code"] = this.txtDjh.Text;

                if (tM.Shipping.shippingDate != null)
                {
                    this.dtpOutDate.Value = Convert.ToDateTime(tM.Shipping.shippingDate);
                    NewMainRow["shippingDate"] = this.dtpOutDate.Value;
                }

                if (tM.Shipping.department != null)
                {
                    SelectItem item = new SelectItem();
                    item.Name = tM.DepartmentName;
                    item.Key = tM.DepartmentCode;
                    item.Value = tM.Shipping.department.ToString();
                    this.txtDepartment.SetSelected(item);

                    NewMainRow["department"] = tM.Shipping.department;
                }

                if (tM.Shipping.businessman != null)
                {
                    SelectItem item = new SelectItem();
                    item.Name = tM.BusinessName;
                    item.Key = tM.BusinessCode;
                    item.Value = tM.Shipping.businessman.ToString();
                    //this.txtBusinessman.SetSelected(item);

                    NewMainRow["businessman"] = tM.Shipping.businessman;
                }

                if (tM.Shipping.@operator != null)
                {
                    this.txtOperator.Text = tM.OperatorName;
                    NewMainRow["operator"] = tM.Shipping.@operator;
                }

                this.txtMender.Text = tM.MenderName;//修改人
                NewMainRow["mender"] = SysLoginUserInfo.UserId;

                this.txtMemo.Text = tM.Shipping.memo;
                NewMainRow["memo"] = tM.Shipping.memo;

                NewMainRow["isVerify"] = tM.Shipping.isVerify;
                this.txtStatus.Text = tM.Shipping.status;
                NewMainRow["status"] = this.txtStatus.Text;

                //this.cbType.Text = tM.RecvMaterial.type;
                //NewMainRow["type"] = this.cbType.Text;

                if (tM.Shipping.updateTime != null)
                    this.dtpUpdatetime.Value = Convert.ToDateTime(tM.Shipping.updateTime);
                if (tM.Shipping.verifyTime != null)
                    this.dtpvertime.Value = Convert.ToDateTime(tM.Shipping.verifyTime);
                this.txtAccessor.Text = tM.AssessorName;

                tMain.Rows.Add(NewMainRow);
            }
            #endregion

            #region
            if (tD != null)
            {
                tDetail.Clear();
                for (int i = 0; i < tD.Count(); i++)
                {
                    DataRow dr = tDetail.NewRow();
                    dr["id"] = tD[i].ShippingDetail.id;
                    dr["shipping"] = tD[i].ShippingDetail.shipping;
                    dr["shippingCode"] = tD[i].ShippingDetail.shippingCode;
                    if (tD[i].ShippingDetail.order != null)
                        dr["order"] = tD[i].ShippingDetail.order;
                    dr["orderCode"] = tD[i].ShippingDetail.orderCode;
                    dr["material"] = tD[i].ShippingDetail.material;
                    dr["materialCode"] = tD[i].ShippingDetail.materialCode;
                    dr["materialName"] = tD[i].MaterialName;
                    dr["specs"] = tD[i].Specs;
                    dr["shippingNum"] = tD[i].ShippingDetail.shippingNum;
                    dr["batchNo"] = tD[i].ShippingDetail.batchNo;
                    dr["depot"] = tD[i].ShippingDetail.depot;
                    dr["depotName"] = tD[i].DepotName;
                    dr["memo"] = tD[i].ShippingDetail.memo;
                    if (tD[i].ShippingDetail.orderDetailId != null)
                        dr["orderDetailId"] = tD[i].ShippingDetail.orderDetailId;
                    if (tD[i].ShippingDetail.prevId != null)
                        dr["qcId"] = tD[i].ShippingDetail.prevId;
                    dr["qcCode"] = tD[i].ShippingDetail.prevCode;
                    dr["unit"] = tD[i].Unit;
                    tDetail.Rows.Add(dr);
                }

                dtpData.DataSource = tDetail;// tD.ToDataTable();
            }
            #endregion

            if (this.txtStatus.Text.ToString() == "已批")
            {
                //审批后,不允许再修改
                EditStatus = Common.BillOperateType.View;
                SetControlsStatus(false);
            }
            else if (this.txtStatus.Text.ToString() == "未批")
            {
                //未审批,允许再修改
                EditStatus = Common.BillOperateType.Update;
                SetControlsStatus(true);
            }
        }

        private void 来自生产单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FindBillFrm frm = new FindBillFrm();
            frm.AllowMulitSelect = false;
            frm.SelectBillType = GRWL.BZSC.Common.EnumCommon.BillType.SCD;
            //frm.BillPrefix = "SCSH";
            frm.Text = "选单(来自生产单)";

            frm.ShowDialog();

            if (frm.DialogResult == DialogResult.OK)
            {
                if (frm.SelectedItems != null)
                {
                    for (int i = dtpData.Rows.Count - 1; i >= 0; i--)
                    {
                        dtpData.Rows.RemoveAt(i);
                    }
                    QProduction qpro = OTConverter.ConvertToObject<QProduction>(frm.SelectedItems);
                    BillFromProduction(qpro.Id);
                }
            }
        }

        private void BillFromProduction(int BillId)
        {
            //XProduction xMain = BaseDataProxy.GetXProduction(BillId);
            XPrdnDetail[] xDetials1 = BaseDataProxy.GetXPrdnDetails(BillId);

            tDetail.Clear();
            if ((xDetials1 != null) && (xDetials1.Count() > 0))
            {
                foreach (XPrdnDetail tbItem in xDetials1)
                {
                    DataRow dr = tDetail.NewRow();
                    //dr["id"]=tbItem.PrdnDetail.id;
                    //dr["shipping"]=tbItem.PrdnDetail.
                    dr["shippingCode"] = this.txtDjh.Text;
                    if (tbItem.PrdnDetail.orderId != null)
                        dr["order"] = tbItem.PrdnDetail.orderId;
                    dr["orderCode"] = tbItem.PrdnDetail.orderCode;
                    dr["material"] = tbItem.PrdnDetail.material;
                    dr["materialCode"] = tbItem.PrdnDetail.materialCode;
                    dr["materialName"] = tbItem.MaterialName;
                    dr["specs"] = tbItem.PrdnDetail.specs;
                    dr["unit"] = tbItem.Unit;
                    if (tbItem.PrdnDetail.finishNum == null)
                        dr["shippingNum"] = 0;
                    else
                        dr["shippingNum"] = tbItem.PrdnDetail.finishNum;
                    //dr["batchNo"]=tbItem.
                    //dr["depot"]=;
                    //dr["depotName"]=;
                    dr["memo"] = tbItem.PrdnDetail.memo;
                    if (tbItem.PrdnDetail.orderDetailId != null)
                        dr["orderDetailId"] = tbItem.PrdnDetail.orderDetailId;
                   // if (tbItem.PrdnDetail.id != null)
                        dr["qcId"] = tbItem.PrdnDetail.id;
                    dr["qcCode"] = tbItem.PrdnDetail.prdnCode;
                    tDetail.Rows.Add(dr);
                }
            }
            this.dtpData.DataSource = tDetail;
        }

        private void 来自质检单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FindBillFrm frm = new FindBillFrm();
            frm.AllowMulitSelect = false;
            frm.SelectBillType = GRWL.BZSC.Common.EnumCommon.BillType.SCZJ;
            frm.BillPrefix = "SCZJ";
            frm.Text = "选单(来自质检单)";

            frm.ShowDialog();

            if (frm.DialogResult == DialogResult.OK)
            {
                if (frm.SelectedItems != null)
                {
                    for (int i = dtpData.Rows.Count - 1; i >= 0; i--)
                    {
                        dtpData.Rows.RemoveAt(i);
                    }
                    QProduction qpro = OTConverter.ConvertToObject<QProduction>(frm.SelectedItems);
                    BillFromQC(qpro.Id);
                }
            }
        }

        private void BillFromQC(int BillId)
        {
            //XProduction xMain = BaseDataProxy.GetXProduction(BillId);
            ////XPrdnDetail[] xDetials1 = BaseDataProxy.GetXPrdnDetails(BillId);
            XQCDetail[] xDetials1 = BaseDataProxy.GetXQCDetials(BillId);
            //XQCDetail[] td = BaseDataProxy.GetXQCDetials(BillId);

            tDetail.Clear();
            if ((xDetials1 != null) && (xDetials1.Count() > 0))
            {
                foreach (XQCDetail tbItem in xDetials1)
                {
                    DataRow dr = tDetail.NewRow();
                    //dr["id"]=tbItem.PrdnDetail.id;
                    //dr["shipping"]=tbItem.PrdnDetail.
                    dr["shippingCode"] = this.txtDjh.Text;
                    //dr["order"] = tbItem.QCDetail.orderId;//质检单无订单id
                    //dr["orderCode"] = tbItem.QCDetail.orderCode;//质检单无订单号
                    dr["material"] = tbItem.QCDetail.material;
                    dr["materialCode"] = tbItem.QCDetail.materialCode;
                    dr["materialName"] = tbItem.MaterialName;
                    dr["specs"] = tbItem.QCDetail.specs;
                    dr["unit"] = tbItem.Unit;
                    dr["shippingNum"] = tbItem.QCDetail.inNum;
                    dr["memo"] = tbItem.QCDetail.memo;
                    //dr["orderDetailId"] = tbItem.QCDetail.orderDetailId;//质检单无订单明细id
                    //if (tbItem.QCDetail.id != null)
                        dr["qcId"] = tbItem.QCDetail.id;
                    dr["qcCode"] = tbItem.QCDetail.qcCode;
                    tDetail.Rows.Add(dr);
                }
            }
            this.dtpData.DataSource = tDetail;
        }

        private void dtpData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int irow = e.RowIndex;
            int icol = e.ColumnIndex;
            if (irow < 0 || icol < 0)
            {
                return;
            }
            else if (icol == 10)// 注意：仓库名称列序号为10，当改变列顺序时，需要调整
            {
                DepotFrm frm = new DepotFrm();
                frm.Text = "选择仓库";
                frm.SearchMode();
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    dtpData.Rows[irow].Cells[9].Value = frm.SelectorItem.Value;
                    dtpData.Rows[irow].Cells[10].Value = frm.SelectorItem.Name;

                    if (!bGraph && irow != (dtpData.Rows.Count - 1))
                    {
                        bGraph = true;
                        if (MessageBox.Show("以下仓库是否设为相同？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            for (int i = dtpData.Rows.Count - 1; irow < i; i--)
                            {
                                dtpData.Rows[i].Cells[9].Value = frm.SelectorItem.Value;
                                dtpData.Rows[i].Cells[10].Value = frm.SelectorItem.Name;
                            }
                        }
                        else
                        {
                            bGraph = false;
                            return;
                        }
                        bGraph = false;
                    }
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if ((bool)tMain.Rows[0]["isVerify"] == true)
            {
                MessageBox.Show("无法删除已审批单据.");
                return;
            }

            if ((EditStatus != Common.BillOperateType.Add) & (EditStatus != Common.BillOperateType.Update))
                return;

            int BillId = Common.StringToInt(tMain.Rows[0]["id"].ToString());
            string message = "是否删除单据？";
            string caption = "确认";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result = MessageBox.Show(message, caption, buttons);
            if (result != DialogResult.Yes)
                return;

            if (BaseDataProxy.DeleteShipping(BillId))
            {
                MessageBox.Show("已成功删除单据.");
                EditStatus = Common.BillOperateType.View;
                setReadOnly(false);
                SetNull();
            }
            else
                MessageBox.Show("无法删除单据.");
        }
    }
}
